import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, List, Brands, Detail, FaultItem, VehicleSold, ProductCategory, DutyUnit} from './DTO';
const baseUrl = '/afterSales/api/v1';
import {baseDataStatus} from '../Enum';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '售后技术支持管理单'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});
const exportListDesc = formatMessage({
    id: 'api.message.exportListDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});
const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});
const submitDesc = formatMessage({
    id: 'api.message.submitDesc',
    defaultMessage: '提交{resourceName}'
}, {resourceName});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});
const solvedDesc = formatMessage({
    id: 'api.message.solvedDesc',
    defaultMessage: '已解决{resourceName}'
}, {resourceName});
const closeDesc = formatMessage({
    id: 'api.message.closeDesc',
    defaultMessage: '关闭{resourceName}'
}, {resourceName});
const transferToTechnicalCaseDesc = formatMessage({
    id: 'api.message.transferToTechnicalCaseDesc',
    defaultMessage: '转技术案例{resourceName}'
}, {resourceName});
const replyDesc = formatMessage({
    id: 'api.message.replyDesc',
    defaultMessage: '回复{resourceName}'
}, {resourceName});
const reportAgainDesc = formatMessage({
    id: 'api.message.reportAgainDesc',
    defaultMessage: '再上报{resourceName}'
}, {resourceName});
const feedbackDesc = formatMessage({
    id: 'api.message.feedbackDesc',
    defaultMessage: '反馈{resourceName}'
}, {resourceName});

const vehicleSoldDesc = formatMessage({
    id: 'api.message.vehicleSoldDesc',
    defaultMessage: '车辆售后档案查询'
});

const faultItemDesc = formatMessage({
    id: 'api.message.faultItemDesc',
    defaultMessage: '故障代码查询'
});
const dutyUnitDesc = formatMessage({
    id: 'api.message.dutyUnitDesc',
    defaultMessage: '供应商查询'
});
const productCategoryDesc = formatMessage({
    id: 'api.message.productCategoryDesc',
    defaultMessage: '供应商查询'
});

const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});
export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brands)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, brandDesc));
};
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/technicalSupport/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/technicalSupports/forCurrentUser?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};


// 查询详情
export const getDetail = id => request(`${baseUrl}/technicalSupports/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));
// 新增
export const addInfo = data => request(`${baseUrl}/technicalSupports`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/technicalSupports/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 导出列表
export const exportInfo = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/technicalSupports/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportListDesc))
        .then(res => res.data);
};

// 作废
export const abandonInfo = (id, data) => request(`${baseUrl}/technicalSupports/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

// 提交/下发
export const submitInfo = (id, data) => request(`${baseUrl}/technicalSupports/${id}/submit`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));

// 已解决
export const solvedInfo = (id, data) => request(`${baseUrl}/technicalSupports/${id}/solved`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, solvedDesc));
// 关闭
export const closeInfo = (id, data) => request(`${baseUrl}/technicalSupports/${id}/close`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, closeDesc));
// 技术转案例
export const transferToTechnicalCaseInfo = (id, data) => request(`${baseUrl}/technicalSupports/${id}/transferToTechnicalCase`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, transferToTechnicalCaseDesc));
// 回复
export const replyInfo = (id, data) => request(`${baseUrl}/technicalSupports/${id}/reply`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, replyDesc));
// 再上报
export const reportAgainInfo = (id, data) => request(`${baseUrl}/technicalSupports/${id}/reportAgain`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, reportAgainDesc));
// 反馈
export const feedbackInfo = (id, data) => request(`${baseUrl}/technicalSupports/${id}/feedback`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, feedbackDesc));
// 车辆售后档案查询
export const getVehicleSold = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/vehicleSolds/forTechnicalSupport?${queryString}`, null, VehicleSold)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, vehicleSoldDesc));
};
// 故障代码查询
export const getFaultItem = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/faultItems/forTechnicalSupport?${queryString}`, null, FaultItem)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, faultItemDesc));
};
// 供应商查询
export const getDutyUnit = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/dutyUnits/forTechnicalSupport?${queryString}`, null, DutyUnit)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, dutyUnitDesc));
};
// 产品分类查询
export const getProductCategory = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/productCategories/forTechnicalSupport?${queryString}`, null, ProductCategory)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryDesc));
};


const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询服务站信息'
});

// 查询服务站
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forServiceInformation?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};


//根据条件查询总代子公司
export const getDealerSimple = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forServiceInformation/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};
